package com.train.servlet;

import com.alibaba.fastjson.JSONObject;
import com.train.service.MovieCountServiceImpI;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/MovieCount")
public class MovieCountServlet extends HttpServlet {
    private MovieCountServiceImpI movieCountServiceImpI=new MovieCountServiceImpI();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List movieOrder = movieCountServiceImpI.getMovieOrder();
        //获取JSON对象
        JSONObject json = new JSONObject();
        if(movieOrder.size()==0){
            json.put("code",300);
            json.put("msg","error");
        }else{
            json.put("code",200);
            json.put("msg","OK");
            json.put("data",movieOrder);
        }

        //把放入JSON中的数据响应给前端
        System.out.println(json);
//        System.out.println(request.getContextPath());
        response.getWriter().write(json.toString());
    }
}
